#include "application.h"
#include "separator.h"

/**
 * @brief	工作状态机切换
 * @note	只进行状态切换，不进行任何操作，在主循环中调用。
 */
void work_flow(separator_t *separator, work_state_t *state)
{
    switch (*state)
    {
    case standby:
        if (separator->can_data.driect_control_cmd == 1)
        {
            *state = driect_control;
        }
        else
        {
            if (separator->engine_state == engine_start)
            {
                *state = press_loop;
            }
            else
            {
                *state = standby; // 不满足切换条件，保持待机状态
            }
        }
        break;
    case driect_control:
        if (separator->can_data.driect_control_cmd == 0)
        {
            if (separator->engine_state == engine_start)
            {
                *state = press_loop;
            }
            else
            {
                *state = standby;
            }
        }
        else
        {
            *state = driect_control; // 不满足切换条件，保持直控状态
        }
    // 压力闭环
    case press_loop:
        if (separator->can_data.driect_control_cmd == 1)
        {
            *state = driect_control;
        }
        else
        {
            if (separator->engine_state == engine_stop)
            {
                *state = delay_stop;
            }
            else
            {
                if (separator->can_data.engine_speed_msg == message_no_received)
                {
                    *state = error;
                }
                else
                {
                    *state = press_loop; // 不满足切换条件，保持在压力闭环
                }
            }
        }
        break;
    // 延迟停机（120s）
    case delay_stop:
        if (separator->can_data.driect_control_cmd == 1)
        {
            *state = driect_control;
        }
        else
        {
            if (separator->engine_state == engine_start)
            {
                *state = press_loop;
            }
            else
            {
                *state = delay_stop; // 不满足切换条件，保持在延迟关机
            }
        }
        break;
    // 错误状态
    case error:
        *state = error; // 进入错误状态，复位方可消除
        break;

    default:
        break;
    }
}
